﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Newtonsoft.Json;

using System.IO;

namespace chad.azure.diagnostics.data
{
    public class ConfigurationFileProcessor
    {
        /*
        public ConfigurationFileProcessor(String jsonObjectString)
        {
            JsonConvert.DeserializeObject(jsonObjectString);
        }
        */
        #region Json

        public static ReportConfiguration GetReportConfigurationFromJsonFile(String jsonFileFullPath)
        {
            //TODO: Verify that the file exists first
            return GetReportConfigurationFromJson(new StreamReader(jsonFileFullPath).ReadToEnd());
        }

        public static ReportConfiguration GetReportConfigurationFromJson(String jsonText)
        {
            return JsonConvert.DeserializeObject<ReportConfiguration>(jsonText);
        }

        #endregion

        #region Stuff

        public static String GenerateReportConfigurationTemplate()
        {
            ReportConfiguration config = new ReportConfiguration();

            config.DateTimeEndMinutesOffset = 0;
            config.DateTimeStartMinutesOffset = -60;

            config.ReportName = "Test Report Name";
            config.ReportDescription = "Test Report Description";
            config.ReportFileName = "testreport";

            // Datasources
            ReportDataset ds1 = new ReportDataset();
            ds1.DatasetName = "DataSetName1";
            ds1.StorageAccountName = "STORAGEACCOUNT_NAME";
            ds1.StorageAccountKey = "STORAGEACCOUNT_KEY";

            ReportDataset ds2 = new ReportDataset();
            ds2.DatasetName = "DataSetName2";
            ds2.StorageAccountName = "STORAGEACCOUNT_NAME";
            ds2.StorageAccountKey = "STORAGEACCOUNT_KEY";

            config.ReportDatasets = new List<ReportDataset>();
            config.ReportDatasets.Add(ds1);
            config.ReportDatasets.Add(ds2);

            // Pages
            ReportPage page1 = new ReportPage();
            page1.DatasetName = "DataSetName1";
            page1.PageTitle = "Test Page 1 Events Summary";
            page1.SheetName = "EventsSummary";
            page1.PageType = ReportPageType.EventSummary;
            page1.Parameters = new Dictionary<string, object>();
            page1.Parameters.Add("ReportSummaries", "ByDeploymentByRoleByMessage");

            ReportPage page2 = new ReportPage();
            page2.DatasetName = "DataSetName1";
            page2.PageTitle = "Test Page 2 Events Detail";
            page2.SheetName = "EventsDetail";
            page2.PageType = ReportPageType.EventDetail;
            page2.Parameters = new Dictionary<string, object>();
            page2.Parameters.Add("EventMessageFilterRegexMatch","");

            config.ReportPages = new List<ReportPage>();
            config.ReportPages.Add(page1);
            config.ReportPages.Add(page2);

            config.ReportOutput = new ReportOutput();
            config.ReportOutput.FileOutputPath = @"c:\Scratch";
            config.ReportOutput.SmtpServerName = "mail.domain.com";
            config.ReportOutput.SmtpServerPort = 25;
            config.ReportOutput.FromEmailAddess = "do-not-reply@domain.com";
            config.ReportOutput.ToEmailAddresses = new string[1];
            config.ReportOutput.ToEmailAddresses[0] = "youremail1@domain.com";
            config.ReportOutput.ToEmailAddresses[0] = "youremail2@domain.com";
            config.ReportOutput.DeleteFileAfterSendingEmail = false;

            // Write It
            return JsonConvert.SerializeObject(config);

        }

        public static object GetParameterFromDictionaryOrDefaultValue(Dictionary<String, Object> dict, String keyName, Object defaultValue)
        {
            if (dict != null && dict.ContainsKey(keyName))
            {
                return dict[keyName];
            }
            else
            {
                return defaultValue;
            }
        }


        #endregion
    }
}
